<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            配送接收
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="padding-right: 100px;">配送任务单号</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="请扫描配送任务单号" id="distributeTaskCode" value=""
                                onclick="getIdSelector();">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">目标仓库</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 仓库" id="workCenterCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter()"></i>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">调入库位</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">物料</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 物料" id="mrlCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectMrl()"></i>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width:150px">任务创建日期</div>
                        <div class="aui-list-item-input" onclick="timePicker();">
                            <input type="text" placeholder="选择日期" style="padding-left: 35px;" class="aui-input"
                                id="dateTime">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">箱码</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="请扫描箱码" id="tm" value="" onclick="getIdSelector();">
                        </div>
                    </div>
                </li>
            </ul>
            <div style="padding-top:2%;"></div>
            <div class="aui-btn aui-btn-info aui-btn-block" onclick="search()">查询</div>
        </div>
        <ul class="ui-selectee" id="tbodyHtml">

        </ul>



    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称
    var mrlCode = ""; //物料编码

    //初始化查询
    apiready = function () {

        $("#distributeTaskCode").focus();
        idSelector = "distributeTaskCode";
        //search();
        //查询条码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //1：仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

                //扫描条码
            } else if (idSelector == 'distributeTaskCode') {
                $("#distributeTaskCode").val(ret);
                tmSplit2(ret);
            } else if (idSelector == 'mrlCode') {

                $("#mrlCode").val(ret);
                getMrl(ret, idSelector);
            } else if (idSelector == 'mrlCode') {

                $("#mrlCode").val(ret);
                getMrl(ret, idSelector);
            } else if (idSelector == 'tm') {

                $("#tm").val(ret);
                search();
            }



        });
    }

    //箱号监听回车事件
    $("#tm").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        // console.log('箱号扫描',tm)
        if (keycode == '13') {

            if (tm != null && tm != "" && tm != undefined) {
                search();
            }
        }
    });

    //6：选择物料
    function goSelectMrl() {
        api.openWin({
            name: 'uwmSelectMrl',
            url: 'uwmSelectMrl.html',
            pageParam: {
                name: "paAndonMrl"
            }
        });
    }

    //物料选择后回调函数
    function setMrl(id, code, name) {
        mrlId = id;
        mrlCode = code;
        mrlName = name;
        document.getElementById('mrlCode').value = code + ":" + name;
    }

    //PDA自带扫描回车事件-库位
    $("#mrlCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //物料编码
            $("#mrlCode").val(sm);
            //校验改箱码是否存在
            getMrl(sm, "mrlCode");
        }
    });

    //物料扫描校验
    function getMrl(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMdMrlList";
        var data = {
            values: {
                codeQ: code,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    mrlId = list[0].sysid;
                    mrlCode = list[0].code;
                    mrlName = list[0].name;
                    document.getElementById(inputId).value = list[0].code + ":" + list[0].name;

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                    console.log("scanRule:" + scanRule);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    //扫描之后条码解析
    function tmSplit2(sm) {
        var arr = sm.split(",");
        $("#distributeTaskCode").val(sm);

        if (arr[0] == null || arr[0] == undefined) {
            $("#distributeTaskCode").val("");
        } else {
            $("#distributeTaskCode").val(arr[0]);
        }

    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#dateTime").val(date);
            }
        });
    }


    //1：仓库扫描校验
    function getWorkCenter(code, inputId) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCenter";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: code,
                mrlCode: "",
                lotCode: "",
                qty: "",
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCenterGid = list[0].workCenterGid;
                    workCenterCode = list[0].workCenterCode;
                    workCenterName = list[0].workCenterName;
                    document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCenterCode").focus();
                    $("#workCenterCode").select();
                }
            }
        });
    }


    //选中仓库
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "paAndonMrl",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterGid = id;
        workCenterCode = code;
        workCenterName = name;
        //$("#workCenterCode").attr("value",code+":"+name);
        document.getElementById('workCenterCode').value = code + ":" + name;
        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }

    //PDA自带扫描回车事件
    $("#workCenterCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCenterCode").val(sm);
            // console.log("扫描回车事件：" + sm);
            //校验改箱码是否存在
            getWorkCenter(sm, "workCenterCode");
        }
    });


    //库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    backtype(workCellGid)

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }


    //选择库位
    function goSelectWorkCell() {
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "paAndonMrl",
                tm: "",
                workCenterGid: workCenterGid,
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;
    }

    //PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });


    function search() {
        var distributeTaskCode = $("#distributeTaskCode").val();
        var dateTime = $("#dateTime").val();
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchPatask";
        var un = localStorage.getItem('un');

        var tm = $("#tm").val();
        //箱码非空校验
        if (tm == "" || tm == null || tm == undefined) {
            api.toast({
                msg: '请扫描/输入 箱码',
                duration: 3000
            });
            return;
        }
        // console.log('tm',tm)
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: workCenterCode,
                workCellCode: workCellCode,
                mrlCode: mrlCode,
                distributeTaskCode: distributeTaskCode,
                createDate: dateTime,
                tm: tm,
                un: un

            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                //$("#tm").val("");
                $("#tm").focus();
                $("#tm").select();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    // console.log('list:',JSON.stringify(myobj.list))
                    setTable(myobj.list)
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $api.html($api.byId("tbodyHtml"), "");
                }
            }
        });
    }


    function setTable(list) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            html += "<li class='sortable ui-selectee ui-sortable' >";
            html += "<div class='white_background'>"
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee' style='color: black !important;background-color: #addbef !important;'>";
           
            html += "<div class='aui-media-list-item-inner ui-selectee ' >";
            html += "<div class='aui-list-item-inner ui-selectee '   onclick='goPage(\"" + list[i].distributeTaskCode + "," + list[i].workCenterCode + "," + list[i].workCellCode + "\");'>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title '>配送单号:</div>";
            html += "<div class='aui-list-item-inner ui-selectee '>" + list[i].distributeTaskCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>目标仓库:</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].workCenterCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>调入库位:</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].workCellCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>创建时间:</div>";
            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].create_date + "</div>";
            html += "</div>";
            //html += "<div class='aui-list-item-text ui-selectee'>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料编码:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style = 'word-wrap:break-word; overflow:hidden;'>" + list[i].mrlCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>规格:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style = 'word-wrap:break-word; overflow:hidden;'>" + list[i].drawing + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 25px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名称:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style = 'word-wrap:break-word; overflow:hidden;'>" + list[i].mrlName + "</div>";
            html += "</div>";

            html += "</div>";
            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</div>";
            html += "</li>";
        }
        $api.html($api.byId("tbodyHtml"), html);
    };

    function goPage(str) {
        var arr = str.split(",");
        api.openWin({
            name: 'paSaveTask',
            url: 'paSaveTask.html',
            pageParam: {
                flag: "in_rk",
                name: "paAndonMrl",
                distributeTaskCode: arr[0],
                workCenterCode: arr[1],
                workCellCode: arr[2]
            }

        });
    }


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

</script>


</html>